package kr.co.iniline.rmkb.core;

import java.util.ArrayList;

import kr.co.iniline.rumkb.server.RmkbUtil;

import com.badlogic.gdx.math.Rectangle;


public class ClientUser {
	String userID = null;
	Kub ownKubs[] = new Kub[0];
	
	public ClientUser(String id) {
	}

	public int getKubNum() {
		return ownKubs.length;
	}
	
	public Kub getKub(int index) {
		return ownKubs[index];
	}
	
	public int getKubTotalPoint() {
		return 0;
	}
	
	public void addKub(int kub) {
		addKubs( new int[]{kub} );
	}
	
	public void addKubs(int kubs[]) {
		Kub newKubs[] = new Kub[ownKubs.length + kubs.length];
		System.arraycopy(ownKubs, 0, newKubs, 0, ownKubs.length);
		for (int i = ownKubs.length; i < newKubs.length; i++) {
			newKubs[i] = new Kub( kubs[i-ownKubs.length] );
		}
		ownKubs = newKubs;
	}
	
	public boolean hasKub(int kub) {
		return hasKubs(new int[]{kub});
	}
	
	public boolean hasKubs(int kubs[]) {
		for (int i = 0; i < kubs.length; i++) {
			boolean flag = false;
			for (int j = 0; j < ownKubs.length; j++) {
				if( kubs[i] == kubs[j] ) {
					flag = true;
				}
			}
			if( !flag ) return false;
		}
		return true;
	}
	
	public void removeKub(int kub) {
		removeKubs(new int[]{kub});
	}
	
	public void removeKubs(int kubs[]) {
		Kub newKubs[] = new Kub[ownKubs.length - kubs.length];
		int count = 0;
		for (int i = 0; i < ownKubs.length; i++) {
			boolean isFind = false;
			Kub orgKub = ownKubs[i];
			for (int j = 0; j < kubs.length; j++) {
				if( kubs[j] == orgKub.data ) {
					isFind = true;
					break;
				}
			}
			if( !isFind ) {
				newKubs[ count++ ] = orgKub;
			}
		}
		ownKubs = newKubs;
	}
	
	public Kub[] getSelectedKubs() {
		ArrayList<Kub> arrayList = new ArrayList<Kub>();
		for (int i = 0; i < getKubNum(); i++) {
			Kub kub = getKub(i);
			if( kub.isSelected() ) {
				arrayList.add( kub );
			}
		}
		if( arrayList.size() == 0 ) return null;
		return arrayList.toArray(new Kub[arrayList.size()]);
	}
	
	public void removeKubs(Kub kubs[]) {
		Kub newKubs[] = new Kub[ownKubs.length - kubs.length];
		int count = 0;
		for (int i = 0; i < ownKubs.length; i++) {
			boolean isFind = false;
			Kub orgKub = ownKubs[i];
			for (int j = 0; j < kubs.length; j++) {
				if( kubs[j].equals(orgKub) ) {
					isFind = true;
					break;
				}
			}
			if( !isFind ) {
				newKubs[ count++ ] = orgKub;
			}
		}
		ownKubs = newKubs;
	}
	
	public void debugOwnKub() {
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < getKubNum(); i++) {
			sb.append( RmkbUtil.toKubString(getKub(i).getData()) +", ");
		}
		System.out.println( sb.toString() );
	}
	
	public class Kub {
		private int data = -1;
		private boolean isSelected = false;
		private Rectangle rect = null;
		
		Kub(int data) {
			this.data = data;
			rect = new Rectangle();
		}
		
		public int getData() {
			return data;
		}
		
		public boolean isSelected() {
			return isSelected;
		}
		
		public void reverse() {
			isSelected = !isSelected;
		}
		
		public void setSelected(boolean flag) {
			isSelected = flag;
		}
		
		public void setBoundary(int x, int y, int width, int height) {
			rect.set(x, y, width, height);
		}
		
		public boolean pickTest(int x, int y) {
			if( rect.contains(x, y) ) {
				reverse();
				return true;
			}
			return false;
		}
	}
}
